<?php
function createThumbs($source, $dest, $requiredWidth, $requiredHeight) {
	

	
	$size = getimagesize($source);
	$givenWidth = $size[0];
	$givenHeight = $size[1];
    
    if($givenWidth<=$requiredWidth && $givenHeight<=$requiredHeight)
    {
		return true;
	}
    
    	
    $requiredRatio = $requiredWidth / $requiredHeight;
    $middleWidth = ceil($givenHeight * $requiredRatio);
    if($middleWidth>$givenWidth){
        $requiredRatio = $requiredHeight / $requiredWidth;
        $middleHeight = ceil($givenWidth * $requiredRatio);
        $middleWidth = $givenWidth;
        $y = ceil(($givenHeight - $middleHeight)/2);
    }
    else{
        $middleHeight = $givenHeight;
        $middleWidth = ceil($givenHeight * $requiredRatio);
        $x = ceil(($givenWidth - $middleWidth)/2);
    }
    
	if(@$new_im = imagecreatetruecolor($requiredWidth,$requiredHeight))
	{
		//echo "<br>Image create true color code executes..";
	}
	else
	{
		return false;
		//echo "<br>Error : in Image create true color .".imagecreatetruecolor($requiredWidth,$requiredHeight);
	}
	/*
	
	if ($extention == "jpg" || $extention == "jpeg") { $im = imagecreatefromjpeg($source); }
	elseif ($extention == "gif")
	 { 
	 	$im = imagecreatefromgif($source); 
	 }
	elseif ($extention == "png") {
		echo $extention;
		echo $source; 
		$im = imagecreatefrompng($source); 
		}
	
	else { echo("ERROR: Unknown image source file format"); }
	*/
	$extention = strtolower(substr($source, strlen($source)-3, strlen($source)));
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(@$im=imagecreatefromjpeg($source))
		{
			//echo "<br> Image created from jpeg..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid jpg file thumb");
			</script>
			<?php
			return false;
			//echo "<br> Error while Image creation from jpeg..";
		} 
	}
	elseif ($extention == "gif") 
	{ 
		if(@$im=imagecreatefromgif($source))
		{
			//echo "<br> Image created from gif..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid gif file thumb");
			</script>
			<?php
			return false;
			//echo "<br> Error : while Image creation from gif..";
		}
	}
	elseif ($extention == "png") 
	{ 
		if(@$im=imagecreatefrompng($source))
		{
			//echo "<br> Image created from png..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid png file thumb");
			</script>
			
			<?php
			return false;
			//echo "<br> Error : while Image creation from png..";
		}
	}
	else 
	{ 
		echo("ERROR: Unknown image source file format"); 
		return false;
	}

	
	if(@imagecopyresampled($new_im,$im,0,0,$x,$y,$requiredWidth,$requiredHeight,$middleWidth,$middleHeight))
	{
		//echo "<br> Image now resampled";
	}
	else
	{
		?>
			<script>
				alert("sorry this file is not a valid image file thumb");
			</script>
		<?php
		return false;
	}
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(!@imagejpeg($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid jpg file thumb");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "gif" ) 
	{ 
		if(!@imagegif($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid gif file thumb");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "png") 
	{ 
		if(!@imagepng($new_im,$dest,9))
		{
			?>
			<script>
				alert("sorry the file is not a valid png file thumb");
			</script>
			<?php
			return false;
		}
	}
	return true;	
	//imagejpeg($new_im,$dest,85);
}

function createThumbsforProfiles($source, $dest, $requiredWidth, $requiredHeight,$pref='width') 
{
	//echo "<br> Source file name : ".$source;
	//echo "<br> Destination File name : ".$dest;
	$size = getimagesize($source);
	//echo "<br>Width of Original Pic : ".
	$givenWidth = $size[0]; // 800
	//echo "<br>Height of Original Pic : ".
	$givenHeight = $size[1]; // 600
    
    if($givenWidth<=$requiredWidth && $givenHeight<=$requiredHeight)
    {
    	return true;
	}
    	
    	
    /* for 100*125 */
    //echo "<br>Width Ratio as Required/Given =".$requiredWidth."/".$givenWidth."= ".
	$widthRatio=$requiredWidth/$givenWidth; // 0.125, 0.4166
    //echo "<br>Height Ratio as Required/Given =".$requiredHeight."/".$givenHeight."= ".
	$heightRatio=$requiredHeight/$givenHeight; //0.208, 0.909
    
    $aspectProportion=0;
    if($widthRatio>=$heightRatio)
    {
    	//echo "<br> Width Ratio is higher : ";
		$aspectProportion=$widthRatio;
	}
	else
	{
		//echo "<br> Height Ratio is higher : ";
		$aspectProportion=$heightRatio;
	}
	
	//if($givenWidth>($givenHeight*2)) // to maintain landscape position
//	{
//		$aspectProportion=$widthRatio;
//	}
//	
	if($pref=="width")
	{
		$aspectProportion=$widthRatio;
	}
	//echo "<br>Preserve Ratio : ".$aspectProportion;
	
//echo "<br>Proportionate Width :".
	$proportionateWidth=ceil($givenWidth*$aspectProportion);
//echo "<br>Proportionate Height :".
	$proportionateHeight=ceil($givenHeight*$aspectProportion);
 
	if(@$new_im=imagecreatetruecolor($proportionateWidth,$proportionateHeight))
	{
		//echo "<br>Image create true color code executes..";
	}
	else
	{
		return false;
		//echo "<br>Error : in Image create true color .".imagecreatetruecolor($proportionateWidth,$proportionateHeight);
	}

	$extention = strtolower(substr($source, strlen($source)-3, strlen($source)));
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(@$im=imagecreatefromjpeg($source))
		{
			//echo "<br> Image created from jpeg..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid jpg file");
			</script>
			<?php
			return false;
			//echo "<br> Error while Image creation from jpeg..";
		} 
	}
	elseif ($extention == "gif") 
	{ 
		if(@$im=imagecreatefromgif($source))
		{
			//echo "<br> Image created from gif..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid gif file");
			</script>
			<?php
			return false;
			//echo "<br> Error : while Image creation from gif..";
		}
	}
	elseif ($extention == "png") 
	{ 
	
		if(@$im=imagecreatefrompng($source))
		{
			//echo "<br> Image created from png..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid png file");
			</script>
			
			<?php
			return false;
			//echo "<br> Error : while Image creation from png..";
		}
	}
	else 
	{ 
		echo("ERROR: Unknown image source file format"); 
		return false;
	}

	if(imagecopyresampled($new_im,$im,0,0,0,0,$proportionateWidth,$proportionateHeight,$givenWidth,$givenHeight))
	{
		//echo "<br> Image now resampled";
	}
	else
	{
		?>
			<script>
				alert("sorry this file is not a valid image file");
			</script>
		<?php
		return false;
	}
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(@!imagejpeg($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid jpg file");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "gif" ) 
	{ 
		if(@!imagegif($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid gif file");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "png") 
	{ 
		if(@!imagepng($new_im,$dest,9))
		{
			?>
			<script>
				alert("sorry the file is not a valid png file");
			</script>
			<?php
			return false;
		}
	}
	return true;	
}
function createThumbsforProfiles_widthTransparent($source, $dest, $requiredWidth, $requiredHeight,$pref='width') 
{
	//echo "<br> Source file name : ".$source;
	//echo "<br> Destination File name : ".$dest;
	$size = getimagesize($source);
	//echo "<br>Width of Original Pic : ".
	$givenWidth = $size[0]; // 800
	//echo "<br>Height of Original Pic : ".
	$givenHeight = $size[1]; // 600
    
    if($givenWidth<=$requiredWidth && $givenHeight<=$requiredHeight)
    {
    	return true;
	}
    	
    	
    /* for 100*125 */
    //echo "<br>Width Ratio as Required/Given =".$requiredWidth."/".$givenWidth."= ".
	$widthRatio=$requiredWidth/$givenWidth; // 0.125, 0.4166
    //echo "<br>Height Ratio as Required/Given =".$requiredHeight."/".$givenHeight."= ".
	$heightRatio=$requiredHeight/$givenHeight; //0.208, 0.909
    
    $aspectProportion=0;
    if($widthRatio>=$heightRatio)
    {
    	//echo "<br> Width Ratio is higher : ";
		$aspectProportion=$widthRatio;
	}
	else
	{
		//echo "<br> Height Ratio is higher : ";
		$aspectProportion=$heightRatio;
	}
	
	if($givenWidth>($givenHeight*2)) // to maintain landscape position
	{
		$aspectProportion=$widthRatio;
	}
	
	if($pref=="width")
	{
		$aspectProportion=$widthRatio;
	}
	else{
		$aspectProportion=$heightRatio;
	}
	//echo "<br>Preserve Ratio : ".$aspectProportion;
	
//echo "<br>Proportionate Width :".
	$proportionateWidth=ceil($givenWidth*$aspectProportion);
//echo "<br>Proportionate Height :".
	$proportionateHeight=ceil($givenHeight*$aspectProportion);
 
	if(@$new_im=imagecreatetruecolor($proportionateWidth,$proportionateHeight))
	{
		//echo "<br>Image create true color code executes..";
	}
	else
	{
		return false;
		//echo "<br>Error : in Image create true color .".imagecreatetruecolor($proportionateWidth,$proportionateHeight);
	}

	$extention = strtolower(substr($source, strlen($source)-3, strlen($source)));
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(@$im=imagecreatefromjpeg($source))
		{
			//echo "<br> Image created from jpeg..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid jpg file");
			</script>
			<?php
			return false;
			//echo "<br> Error while Image creation from jpeg..";
		} 
	}
	elseif ($extention == "gif") 
	{ 
		if(@$im=imagecreatefromgif($source))
		{
			//echo "<br> Image created from gif..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid gif file");
			</script>
			<?php
			return false;
			//echo "<br> Error : while Image creation from gif..";
		}
	}
	elseif ($extention == "png") 
	{ 
		if(@$im=imagecreatefrompng($source))
		{
				 
 
        
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid png file");
			</script>
			
			<?php
			return false;
			//echo "<br> Error : while Image creation from png..";
		}
	}
	else 
	{ 
		echo("ERROR: Unknown image source file format"); 
		return false;
	}


	if(imagecopyresampled($new_im,$im,0,0,0,0,$proportionateWidth,$proportionateHeight,$givenWidth,$givenHeight))
	{
		//echo "<br> Image now resampled";
	}
	else
	{
		?>
			<script>
				alert("sorry this file is not a valid image file");
			</script>
		<?php
		return false;
	}
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(@!imagejpeg($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid jpg file");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "gif" ) 
	{ 
		if(@!imagegif($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid gif file");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "png") 
	{ 
		
		// create a new true color image with dimensions $width2 and $height2
        $image2 = imagecreatetruecolor($proportionateWidth, $proportionateHeight);
 
        // preserve transparency for PNG and GIF images
       
          // allocate a color for thumbnail
            $background = imagecolorallocate($image2, 0, 0, 0);
            // define a color as transparent
            imagecolortransparent($image2, $background);
            // set the blending mode for thumbnail
            imagealphablending($image2, false);
            // set the flag to save alpha channel
            imagesavealpha($image2, true);
        
 
        // save thumbnail image to the file
        imagecopyresampled($image2, $im, 0, 0, 0, 0, $proportionateWidth,$proportionateHeight,$givenWidth,$givenHeight);
        $result=imagepng($image2,$dest,9);
		
		if(!$result)
		{
			?>
			<script>
				alert("sorry the file is not a valid png file");
			</script>
			<?php
			return false;
		}
	}
	return true;	
}

function createThumbsforProfiles_widthequal_Transparent($source, $dest, $requiredWidth, $requiredHeight,$pref='') 
{
	//echo "<br> Source file name : ".$source;
	//echo "<br> Destination File name : ".$dest;
	$size = getimagesize($source);
	//echo "<br>Width of Original Pic : ".
	$givenWidth = $size[0]; // 800
	//echo "<br>Height of Original Pic : ".
	$givenHeight = $size[1]; // 600
    
    if($givenWidth<=$requiredWidth && $givenHeight<=$requiredHeight)
    {
    	return true;
	}
    	
    	
    /* for 100*125 */
    //echo "<br>Width Ratio as Required/Given =".$requiredWidth."/".$givenWidth."= ".
	$widthRatio=$requiredWidth/$givenWidth; // 0.125, 0.4166
    //echo "<br>Height Ratio as Required/Given =".$requiredHeight."/".$givenHeight."= ".
	$heightRatio=$requiredHeight/$givenHeight; //0.208, 0.909
    
    $aspectProportion=0;
    if($widthRatio>=$heightRatio)
    {
    	//echo "<br> Width Ratio is higher : ";
		$aspectProportion=$widthRatio;
	}
	else
	{
		//echo "<br> Height Ratio is higher : ";
		$aspectProportion=$heightRatio;
	}
	
	if($givenWidth>($givenHeight*2)) // to maintain landscape position
	{
		$aspectProportion=$widthRatio;
	}
	/*
	if($pref=="width")
	{
		$aspectProportion=$widthRatio;
	}
	else{
		$aspectProportion=$heightRatio;
	}
	*/
	//echo "<br>Preserve Ratio : ".$aspectProportion;
	
//echo "<br>Proportionate Width :".
	//$proportionateWidth=ceil($givenWidth*$aspectProportion);
	$proportionateWidth=$requiredWidth;
//echo "<br>Proportionate Height :".
	//$proportionateHeight=ceil($givenHeight*$aspectProportion);
	$proportionateHeight=$requiredHeight;
 
	if(@$new_im=imagecreatetruecolor($proportionateWidth,$proportionateHeight))
	{
		//echo "<br>Image create true color code executes..";
	}
	else
	{
		return false;
		//echo "<br>Error : in Image create true color .".imagecreatetruecolor($proportionateWidth,$proportionateHeight);
	}

	$extention = strtolower(substr($source, strlen($source)-3, strlen($source)));
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(@$im=imagecreatefromjpeg($source))
		{
			//echo "<br> Image created from jpeg..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid jpg file");
			</script>
			<?php
			return false;
			//echo "<br> Error while Image creation from jpeg..";
		} 
	}
	elseif ($extention == "gif") 
	{ 
		if(@$im=imagecreatefromgif($source))
		{
			//echo "<br> Image created from gif..";
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid gif file");
			</script>
			<?php
			return false;
			//echo "<br> Error : while Image creation from gif..";
		}
	}
	elseif ($extention == "png") 
	{ 
		if(@$im=imagecreatefrompng($source))
		{
				 
 
        
		}
		else
		{
			?>
			<script>
				alert("sorry this file is not a valid png file");
			</script>
			
			<?php
			return false;
			//echo "<br> Error : while Image creation from png..";
		}
	}
	else 
	{ 
		echo("ERROR: Unknown image source file format"); 
		return false;
	}


	if(imagecopyresampled($new_im,$im,0,0,0,0,$proportionateWidth,$proportionateHeight,$givenWidth,$givenHeight))
	{
		//echo "<br> Image now resampled";
	}
	else
	{
		?>
			<script>
				alert("sorry this file is not a valid image file");
			</script>
		<?php
		return false;
	}
	if ($extention == "jpg" || $extention == "jpeg") 
	{ 
		if(@!imagejpeg($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid jpg file");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "gif" ) 
	{ 
		if(@!imagegif($new_im,$dest,85))
		{
			?>
			<script>
				alert("sorry the file is not a valid gif file");
			</script>
			<?php
			return false;
		}
	}
	if ($extention == "png") 
	{ 
		
		// create a new true color image with dimensions $width2 and $height2
        $image2 = imagecreatetruecolor($proportionateWidth, $proportionateHeight);
 
        // preserve transparency for PNG and GIF images
       
          // allocate a color for thumbnail
            $background = imagecolorallocate($image2, 0, 0, 0);
            // define a color as transparent
            imagecolortransparent($image2, $background);
            // set the blending mode for thumbnail
            imagealphablending($image2, false);
            // set the flag to save alpha channel
            imagesavealpha($image2, true);
        
 
        // save thumbnail image to the file
        imagecopyresampled($image2, $im, 0, 0, 0, 0, $proportionateWidth,$proportionateHeight,$givenWidth,$givenHeight);
        $result=imagepng($image2,$dest,9);
		
		if(!$result)
		{
			?>
			<script>
				alert("sorry the file is not a valid png file");
			</script>
			<?php
			return false;
		}
	}
	return true;	
}
?>